<template>
  <el-row type="flex" justify="space-around" align="middle" style="height:60px; width:100%">
    <el-col>
      <span>{{ treeNode.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end" align="middle" style="height:">
        <el-col>
          <span>{{ treeNode.manager }}</span>
        </el-col>
        <el-col>
          <el-dropdown @command="handleCommand">
            <span>操作<i class="el-icon-arrow-down el-icon--right" /></span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="isRoot" command="edit">编辑部门</el-dropdown-item>
              <el-dropdown-item v-if="isRoot" command="del">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartmentsApi } from '@/api/departments'
export default {
  name: 'HrsaasTreeTools',
  props: {
    treeNode: {
      type: Object,
      default: () => ({})
      // default: () => {
      //   return {}
      // }
    },
    isRoot: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {

    }
  },
  mounted() {
  },
  methods: {
    handleCommand(type) {
      if (type === 'add') {
        // 添加
        // 如何控制我 的兄弟组件 显示出来
        // 我先到达父组件
        // console.log('==', this.treeNode)
        // 把数据传递给 addDept
        // 先给父亲， 父亲 => addDept
        this.$emit('add', this.treeNode)
        // 在父组件里控制 addDept 显示隐藏
      } else if (type === 'edit') {
        // bianji
        this.$emit('edit', this.treeNode)
      } else {
        // 删除操作
        this.$confirm('确认删除改部门吗', '提示', {
          type: 'warning'
        }).then(async res => {
          // 写删除接口
          // await delDepartmentsApi(this.treeNode.id)
          // promise 链式调用
          return delDepartmentsApi(this.treeNode.id)
          // 删除成功之后 提示 刷新页面
        }).then(res => {
          this.$message.success('删除成功')
          this.$emit('refreshList')
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
